package com.erps.res.rest;

import java.util.List;
import java.util.Map;

import com.erps.CommonUtils.StringEx;
import com.erps.db.DBManager;
import com.erps.sys.rest.ErspResInfoRest;
import com.erps.sys.rest.PageData;
import com.hmkj.entity.ErspResInfo;
import com.hmkj.entity.ErspResStand;

public class ErspResStandRest {
	public static List<Map<String, String>> Query() {

		String strSQL = "select r.res_title,s.res_intro,r.res_view_count,r.last_update_time,r.res_use_count from ersp_res_info r left join ersp_res_stand s on s.id=r.id ";
		List<Map<String, String>> res_list = DBManager.QueryData(strSQL);

		return res_list;

	}

	public PageData Query(int iPageNO, int iPageSize, String iOrderType, String cShowType, String cProZone, String cKeyWord) {
		String cWhereParm = "";
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1) ");
		sql.append(" from ersp_res_info x,ersp_res_stand y ");
		sql.append(" where x.id=y.id ");
		sql.append(" and x.res_release=1 ");
		if (StringEx.getStr(cWhereParm).length() > 0) {
			sql.append(cWhereParm);
		}
		String cOrderBy;

		if (iOrderType.equals("1")) {
			cOrderBy = " order by x.res_order_count desc ";
		} else if (iOrderType.equals("2")) {
			cOrderBy = " order by x.last_update_time desc ";
		} else {
			cOrderBy = " order by x.res_view_count desc ";
		}

		int iRowCount = StringEx.getInt(DBManager.GetStrValue(sql.toString()));
		int iPageCount = (int) Math.ceil(iRowCount / (double) iPageSize);
		if (iPageNO < 0)
			iPageNO = 1;
		else if (iPageNO > iPageCount)
			iPageNO = iPageCount;
		sql.setLength(0);
		sql.append(" select * ");
		sql.append(" from ersp_res_info x,ersp_res_stand y ");
		sql.append(" where x.id=y.id ");
		sql.append(" and x.res_release=1  ");

		if (!cShowType.equals("0")) {
			sql.append(" and x.res_type=" + cShowType);
		}
		if (cProZone != null) {
			switch (StringEx.getInt(cProZone)) {
			case 1:
				sql.append(" and res_popsize<=100 ");
				break;
			case 2:
				sql.append(" and res_popsize>100 and res_popsize<=500 ");
				break;
			case 3:
				sql.append(" and res_popsize>500 and res_popsize<1000 ");
				break;
			case 4:
				sql.append(" and res_popsize>1000 ");
				break;
			default:
				sql.append(" and res_popsize='根据活动情况' ");
				break;
			}
		}

		List<Map<String, String>> dtRows = DBManager.QueryData(sql.toString(), cOrderBy, iPageNO, iPageSize);
		PageData ret = new PageData();
		ret.setPageno(iPageNO);
		ret.setPagecount(iPageCount);
		ret.setRowcount(iRowCount);

		ErspResInfoRest vResDao = new ErspResInfoRest();
		int iLength = (dtRows == null) ? 0 : dtRows.size();
		for (int i = 0; i < iLength; i++) {
			Map<String, String> vo = dtRows.get(i);
			ErspResStand voUser = FindOne(StringEx.getMapVal(vo, "id"));
			ErspResInfo voRes = vResDao.FindOne(StringEx.getMapVal(vo, "id"));
			ret.getKeylist().add(voRes);
			ret.getVallist().add(voUser);
		}

		return ret;
	}

	public ErspResStand FindOne(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_res_stand ");
		sql.append(" where id='" + cKeyID + "'");
		ErspResStand vo = new ErspResStand();

		Map<String, String> rs = DBManager.GetRow(sql.toString());
		vo.setId(StringEx.getMapVal(rs, "id"));
		vo.setRes_id(StringEx.getMapVal(rs, "resid"));
		vo.setRes_addr(StringEx.getMapVal(rs, "res_addr"));
		vo.setRes_areasize(StringEx.getMapVal(rs, "res_areasize"));
		vo.setRes_equipment(StringEx.getMapVal(rs, "res_equipment"));
		vo.setRes_intro(StringEx.getMapVal(rs, "res_intro"));
		vo.setRes_opentime(StringEx.getMapVal(rs, "res_opentime"));
		vo.setRes_popsize(StringEx.getMapVal(rs, "res_popsize"));
		vo.setRes_instrument(StringEx.getMapVal(rs, "res_instrument"));
		try {
			vo.setRes_posx(Double.parseDouble(StringEx.getMapVal(rs, "res_posx")));
			vo.setRes_posy(Double.parseDouble(StringEx.getMapVal(rs, "res_posy")));
		} catch (Exception ex) {

		}

		return vo;
	}

	public Map<String, String> FindAsMap(String cKeyID) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select * ");
		sql.append(" from ersp_res_info x,ersp_res_stand y");
		sql.append(" where x.id=y.id ");
		sql.append(" and x.id='" + cKeyID + "'");

		return DBManager.GetRow(sql.toString());

	}
}
